var reviewOrder = {};
reviewOrder.methods = {};

$(function () {

    // 商品评价star
    reviewOrder.methods.productStarsScore();

    // 物流评价star
    reviewOrder.methods.transportStarsScore();

    // 买家印象
    reviewOrder.methods.buyerThink();

    // 限制文本域输入
    reviewOrder.methods.heartInfoArea();

    // 自定义标签
    reviewOrder.methods.customBtn();

    // 图片上传
    reviewOrder.methods.imgUpload();

    // 删除上传图片
    reviewOrder.methods.imgUploadDelete();

    // 图片切换
    reviewOrder.methods.PictureSwitching();

    // 左右高度
    // reviewOrder.methods.equalHeight();

    // 发表
    reviewOrder.methods.published();

})



// 商品评价star
reviewOrder.methods.productStarsScore = function () {
    var starsPScoreLi = $(".shop-reviewOrder-product-stars li");
    reviewOrder.n = -1;
    for (var i = 0; i < starsPScoreLi.length; i++) {
        starsPScoreLi[i].index = i;
        starsPScoreLi[i].onmouseover = function () {
            reviewOrder.methods.starsPScore(this.index);
        }

        starsPScoreLi[i].onmouseout = function () {
            reviewOrder.methods.starsPScore(reviewOrder.n);
        }

        starsPScoreLi[i].onclick = function () {
            reviewOrder.n = this.index;
            $(".shop-reviewOrder-product-scoreNum").text((reviewOrder.n + 1) * 2 + "分");
        }
    }
}
// 商品评价点亮star
reviewOrder.methods.starsPScore = function (productNum) {
    var starsProductScoreLi = $(".shop-reviewOrder-product-stars li");

    for (var j = 0; j < starsProductScoreLi.length; j++) {
        if (j <= productNum) {
            starsProductScoreLi[j].className = "shop-productStars-active"; //给之前的所有星星点亮
        } else {
            starsProductScoreLi[j].className = ""; //让所有的都不亮
        }
    }

}

// 物流评价star
reviewOrder.methods.transportStarsScore = function () {
    var starsTScoreLi = $(".shop-reviewOrder-transport-stars li");
    reviewOrder.b = -1;
    for (var i = 0; i < starsTScoreLi.length; i++) {
        starsTScoreLi[i].index = i;
        starsTScoreLi[i].onmouseover = function () {
            reviewOrder.methods.starsTScore(this.index);
        }

        starsTScoreLi[i].onmouseout = function () {
            reviewOrder.methods.starsTScore(reviewOrder.b);
        }

        starsTScoreLi[i].onclick = function () {
            reviewOrder.b = this.index;
            $(".shop-reviewOrder-transport-scoreNum").text((reviewOrder.b + 1) * 2 + "分");
        }
    }
}
// 物流评价点亮star
reviewOrder.methods.starsTScore = function (transportNum) {
    var starsTransportScoreLi = $(".shop-reviewOrder-transport-stars li");
    for (var x = 0; x < starsTransportScoreLi.length; x++) {
        if (x <= transportNum) {
            starsTransportScoreLi[x].className = "shop-transportStars-active";
        } else {
            starsTransportScoreLi[x].className = "";
        }
    }

}

// 买家印象
reviewOrder.methods.buyerThink = function () {
    $(".shop-buyer-txt-btn").on("click", function () {
        $(this).toggleClass("shop-buyer-click");
    })
}

// 限制文本域输入
reviewOrder.methods.heartInfoArea = function () {
    $(".shop-reviewOrder-heart-info-area").on("input propertychange", function () {
        var $this = $(this),
            _val = $this.val(),
            count = "";
        if (_val.length > 500) {
            $this.val(_val.substring(0, 500));
        }
        count = 500 - $this.val().length;
        $(".shop-reviewOrder-heart-info-count").text(count);
    });
}

// 左右高度
// reviewOrder.methods.equalHeight = function () {
//     var rightCon = $(".shop-reviewOrder-con-right").height();
//     $(".shop-reviewOrder-con-left").height(rightCon);
// }

// 自定义标签
reviewOrder.methods.customBtn = function () {
    $(".shop-buyer-txt-btn-last > input").on("focus", function () {
        if ($(this).val() == "自定义标签") {
            $(this).val("");
            $(this).removeClass("shop-input-plus");
            $(this).css("text-align", "left");
        }
    });
    // 搜索框失去焦点
    $(".shop-buyer-txt-btn-last > input").on("focusout", function () {
        if ($(this).val() == "" || $(this).val().trim() == "") {
            $(this).css("text-align", "center");
            $(this).val("自定义标签");
            $(this).addClass("shop-input-plus");
        }
    });

}


// 图片上传
reviewOrder.methods.imgUpload = function () {
    $(".shop-reviewOrder-input-file").find("input").on("change", function () {
        var s = $(this).val();
        var start = s.indexOf(".") + 1;
        var name = s.substring(start, s.length).toLowerCase();
        if (name != "jpg" && name != "png" && name != "gif" && name != "jpeg") {
            return;
        }

        $(this).prop('type', 'file');
        var file = $(this)[0].files[0];

        var reader = new FileReader();

        var imgFile;

        reader.onload = function (e) {
            imgFile = e.target.result;
            $(".shop-reviewOrder-uploadImg").append("<a><img src=" + imgFile + " ><span></span></a>");
            Controller.Alert("图片上传成功", true, 1000);
            reviewOrder.methods.imgUploadNum();
            reviewOrder.methods.imgUploadDelete();
        };
        $(this).prop('type', 'text');
        $(this).prop('type', 'file');

        if (file == null) {
            return false;
        } else {
            reader.readAsDataURL(file);
        }
    })
}

// 图片上传数量限制
reviewOrder.methods.imgUploadNum = function () {
    $(".shop-input-file-num span").text($(".shop-reviewOrder-uploadImg").find("a").length);
    if ($(".shop-reviewOrder-uploadImg").find("a").length <= 4) {
        $(".shop-reviewOrder-input-file").show();
    } else {
        $(".shop-reviewOrder-input-file").hide();
        return;
    }
}

// 删除上传图片
reviewOrder.methods.imgUploadDelete = function () {
    $(".shop-reviewOrder-uploadImg a span").on("click", function () {
        $(this).parents("a").remove();
        $(".shop-reviewOrder-uploadImgBig").hide();
        // reviewOrder.methods.equalHeight();
        reviewOrder.methods.imgUploadNum();
    })
}

// 图片切换
reviewOrder.methods.PictureSwitching = function () {
    $(".shop-reviewOrder-upload").on("click", ".shop-reviewOrder-uploadImg a", function () {
        if ($(this).hasClass("shop-img-selected")) {
            $(this).removeClass("shop-img-selected");
            // reviewOrder.methods.equalHeight();
        } else {
            $(this).parents(".shop-reviewOrder-con-right .shop-reviewOrder-uploadImg").find("a").each(function () {
                $(this).removeClass("shop-img-selected");
            });
            $(this).addClass("shop-img-selected");
        }
        reviewOrder.methods.UpdateBigimg($(this));
    });
}

// 放大图
reviewOrder.methods.UpdateBigimg = function (obj) {
    var imgSelected = obj.parents(".shop-reviewOrder-con-right .shop-reviewOrder-uploadImg").find(".shop-img-selected");
    var bigImg = obj.parents(".shop-reviewOrder-con-right").find(".shop-reviewOrder-uploadImgBig");
    if (imgSelected.length == 1) {
        bigImg.css("display", "block");
        bigImg.find("img").attr("src", imgSelected.find("img").attr("src"));
    } else {
        bigImg.css("display", "none");
    }
    // reviewOrder.methods.equalHeight();
}

// 发表
reviewOrder.methods.published = function () {  
    $(".shop-reviewOrder-published").on("click",".shop-reviewOrder-published-btn", function () {
        var spanTips = "<span class='spanTips' style='color:#e64545; margin-left:12px;'>*必选</span>";
        if ($(".shop-reviewOrder-product-stars li").hasClass("shop-productStars-active")) {
            Message.Show("提示", "提交评价", true, true, 5, "sureCancle", reviewOrder.methods.publishedEmpty, reviewOrder.methods.publishedCancel).set;

        } else {
            if($(".shop-reviewOrder-product-score").find("span").hasClass("spanTips")){
                return;
            }
            $(".shop-reviewOrder-product-score").append(spanTips);
        }
    })

    $(".shop-reviewOrder-product-stars").on("click", function () {
        $(".shop-reviewOrder-product-score").find(".spanTips").remove();
    })
}
// 再次发表前清空(确定按钮)
// reviewOrder.methods.publishedEmpty = function () {
//     $(".shop-reviewOrder-product-stars li").removeClass("shop-productStars-active");
//     $(".shop-reviewOrder-product-scoreNum").text("0分");
//     reviewOrder.n = -1;
//     $(".shop-buyer-txt-btn").removeClass("shop-buyer-click");
//     $(".shop-reviewOrder-transport-stars li").removeClass("shop-transportStars-active");
//     reviewOrder.b = -1;
//     $(".shop-reviewOrder-transport-scoreNum").text("0分");
//     $(".shop-reviewOrder-heart-info-area").val("");
//     $(".shop-reviewOrder-heart-info-count").text("0");
//     $(".shop-reviewOrder-uploadImg").html("");
//     $(".shop-reviewOrder-input-file").show();
//     $(".shop-reviewOrder-uploadImgBig").hide();
//     $(".shop-input-file-num span").text("0");
//     reviewOrder.methods.equalHeight();
//     return true;

// }
// (取消按钮)
reviewOrder.methods.publishedCancel = function () {
    return true;
}